package com.itboye.lottery.activity.follow;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.itboye.bylibrary.util.ScreenUtil;
import com.itboye.bylibrary.util.TimeUtils;
import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.daletou.BascketballMainActivity;
import com.itboye.lottery.activity.football.FootballMainActivity;
import com.itboye.lottery.adapter.HotPeopleDetailFollowListAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.bean.BallInfoEntity;
import com.itboye.lottery.bean.FollowOrderDetailBean;
import com.itboye.lottery.bean.MyFollowDetailBean;
import com.itboye.lottery.bean.OrderBean;
import com.itboye.lottery.dialog.PayDialog;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.view.AddDelEditTextView;
import com.itboye.lottery.view.rounded.RoundedImageView;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

import butterknife.BindView;

import static com.itboye.lottery.utils.BaskcketballPrizeH.*;
import static com.itboye.lottery.utils.DisplayBetContent.getBetContentAndPrizeContent;
import static com.itboye.lottery.utils.DisplayBetContent.getLqPrize;

public class FollowDetailActivity extends BaseActivity implements ResponseCallback {

    int type = 3010;
    TextView tv_end_time, btn_follow_order_detail_go_on, tv_follow_detail_project_times, tv_follow_detail_project_mode, tv_follow_detail_project_beishu;
    TextView tv_bet_lock_dec;
    @BindView(R.id.rlLock)
    View rlLock;
    private HotPeopleDetailFollowListAdapter adapter;
    RecyclerView ptr_listView;
    UserPresenter userPresenter = new UserPresenter(this);
    String orderId;
    RoundedImageView iv_user_icon;
    TextView tv_username;
    private FollowOrderDetailBean followOrderDetailBean;
    int orderType = 0;//0:大神跟单详情  1：跟单详情  2：发单详情
    TextView tv_shijishouru, tv_shijishouru_head, tv_group_buy_money_head, tv_status_head, tv_jiangjin_head;
    TextView tv_follow_title;
    TextView tv_status, tv_follow_beizhu, tv_group_buy_money, tv_jiangjin;
    LinearLayout fl_content;
    String lUid;
    LinearLayout ll_follow_container;
    TextView tv_bet;
    RadioGroup rg_group;
    private Integer multiple = 10;
    private MyFollowDetailBean myFollowDetailBean;
    TextView list_item_parent_title;
    LinearLayout ll_order_message;
    ImageView iv_status;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_follow_detail;
    }

    @Override
    protected void initData() {
        /**
         * 大神跟单详情(可见跟单列表，表头认购金额 方案状态 赚取佣金)
         */
        type = getIntent().getIntExtra("type", 1);
        orderType = getIntent().getIntExtra("orderType", 0);
        lUid = getIntent().getStringExtra("lu_id");
        orderId = getIntent().getStringExtra("order_id");
        initTableHeader();
    }

    private void initTableHeader() {
        switch (orderType) {
            case 0:
                setTitle((type == 1 ? "足球跟单方案详情" : "篮球跟单方案详情"));
                tv_shijishouru.setVisibility(View.GONE);
                tv_shijishouru_head.setVisibility(View.GONE);
                fl_content.setVisibility(View.VISIBLE);
                tb_shendan.setVisibility(View.VISIBLE);
                tb_follow.setVisibility(View.GONE);
                userPresenter.getDaShenFollowDetail(orderId);//大神跟单详情
                break;
            case 1:
                setTitle((type == 1 ? "足球跟单方案详情" : "篮球跟单方案详情"));
                tv_group_buy_money_head.setText("奖金收入");
                tv_status_head.setText("佣金扣点");
                tv_jiangjin_head.setText("佣金金额");
                tv_shijishouru.setVisibility(View.VISIBLE);
                tv_shijishouru_head.setVisibility(View.VISIBLE);
                fl_content.setVisibility(View.GONE);
                tb_shendan.setVisibility(View.GONE);
                tb_follow.setVisibility(View.VISIBLE);
                userPresenter.getFollowDetail(orderId, lUid);//跟单详情
                break;
            case 2:
                setTitle((type == 1 ? "足球神单方案详情" : "篮球神单方案详情"));
                tv_group_buy_money_head.setText("奖金收入");//发单详情(数据模型跟大神跟单详情类似)
                tv_status_head.setText("佣金收入");
                tv_jiangjin_head.setText("合计");
                tv_shijishouru.setVisibility(View.GONE);
                tv_shijishouru_head.setVisibility(View.GONE);
                fl_content.setVisibility(View.VISIBLE);
                tb_shendan.setVisibility(View.GONE);
                tb_follow.setVisibility(View.VISIBLE);
                userPresenter.getFaDanDetail(orderId);//发单详情
                break;
        }
    }

    private void updateDaShenUI(FollowOrderDetailBean orderEntity) {
        tv_end_time.setText("");
        tv_end_time.setBackgroundResource(0);
        //中奖状态显示
        switch (orderEntity.getOrderInfo().getIs_result()) {
            case "1":
                switch (orderEntity.getOrderInfo().getLottery_result()) {
                    case "0":
                        tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.zaijiezaili_follow_detail, 0, 0, 0);
                        break;
                    case "1":
                        tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.gongxizhongjiang, 0, 0, 0);
                        break;
                }
                btn_follow_order_detail_go_on.setVisibility(View.VISIBLE);
                ll_follow_container.setVisibility(View.GONE);
                break;
            case "0":
                ll_follow_container.setVisibility(View.GONE);
                btn_follow_order_detail_go_on.setVisibility(View.VISIBLE);
                tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.dengdaikaijian, 0, 0, 0);
                break;
        }
        tv_username.setText(orderEntity.getOrderInfo().getNickname());
        GlideUtil.displayForCircle(this, orderEntity.getOrderInfo().getHead_pic(), R.mipmap.head_default, iv_user_icon);
        tv_follow_beizhu.setText(orderEntity.getOrderInfo().getReason());
        if (orderType == 2) {
            tv_group_buy_money.setText(orderEntity.getOrderInfo().getWin_amount() + "元");
            tv_status.setText(orderEntity.getOrderInfo().getCommission() + "元");//佣金收入
            iv_status.setBackgroundResource(Integer.valueOf(getStatus(orderEntity.getOrderInfo().getTicket_status())[1]));
            iv_status.setVisibility(View.VISIBLE);
            tv_jiangjin.setText(orderEntity.getOrderInfo().getAll_money() + "元");//合计
        } else {
            tv_group_buy_money.setText(orderEntity.getOrderInfo().getTotal_amount() + "元");
            tv_status.setText(getStatus(orderEntity.getOrderInfo().getTicket_status())[0]);
            tv_jiangjin.setText(orderEntity.getOrderInfo().getCommission() + "元");//赚取佣金
        }
        btn_follow_order_detail_go_on.setText("继续购买" + (type == 1 ? "竞彩足球" : "竞彩篮球"));
        updateUICaseDetail(orderEntity);//方案详情
        updateUIFollowList(orderEntity);//显示跟单列表
        ((RadioButton) rg_group.getChildAt(0)).setChecked(true);
        for (int i = 0; i < rg_group.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) rg_group.getChildAt(i);
            radioButton.setOnCheckedChangeListener((compoundButton, b) -> {
                if (b) {
                    caculateTotalMoney(Integer.valueOf(compoundButton.getTag().toString()));
                } else {
//                    caculateTotalMoney(1);
                }
            });
        }
        caculateTotalMoney(multiple);
    }

    AddDelEditTextView addAndDelView;
    TextView tv_bet_total_money;

    private void caculateTotalMoney(Integer integer) {
        this.multiple = integer;
        addAndDelView.setBetNumber(this.multiple);
        tv_bet_total_money.setText("共" + integer * Double.parseDouble(followOrderDetailBean.getOrderInfo().getSingle_amount()) + "元");
    }

    private String[] getStatus(String ticket_status) {
//        2已出票 0未出票 1出票中
        String[] str = new String[]{"", ""};
        switch (ticket_status) {
            case "-1":
                str[0] = "已过期";
                str[1] = R.mipmap.yiguoqi + "";
                break;
            case "0":
                str[0] = "未出票";
                str[1] = R.mipmap.weichupiao + "";
                break;
            case "1":
                str[0] = "出票中";
                str[1] = R.mipmap.chupiaozhong + "";
                break;
            case "2":
                str[0] = "已出票";
                str[1] = R.mipmap.chupiaochenggong + "";
                break;
            case "3":
                str[0] = "出票失败";
                str[1] = R.mipmap.chupiaoshibai + "";
                break;
        }
        return str;
    }


    private void updateUIFollowList(FollowOrderDetailBean orderEntity) {
        tv_follow_title.setText("跟单列表(" + orderEntity.getFollow_user().getFollow_num() + "人," + orderEntity.getFollow_user().getAmount() + "元)");
        adapter = new HotPeopleDetailFollowListAdapter(this, orderEntity.getFollow_user().getFollow_user());
        ptr_listView.setLayoutManager(new LinearLayoutManager(this));
        ptr_listView.setNestedScrollingEnabled(false);
        ptr_listView.setHasFixedSize(true);
        ptr_listView.setAdapter(adapter);

    }

    private void updateUICaseDetail(FollowOrderDetailBean mHotOrederDetail) {

        //场数
        tv_follow_detail_project_times.setText((mHotOrederDetail.getOrderInfo().getBall_info() == null ? 0 : mHotOrederDetail.getOrderInfo().getBall_info().size()) + "场");
        //投注方式
        if (mHotOrederDetail.getOrderInfo().getFree_pass().contains("*")) {
            tv_follow_detail_project_mode.setText(mHotOrederDetail.getOrderInfo().getFree_pass().replaceAll("\\*", "串"));
        } else {
            if (mHotOrederDetail.getOrderInfo().getFree_pass().contains(",")) {
                tv_follow_detail_project_mode.setText(mHotOrederDetail.getOrderInfo().getFree_pass().replaceAll(",", "串1,").substring(0, mHotOrederDetail.getOrderInfo().getFree_pass().length() - 1));
            } else {
                tv_follow_detail_project_mode.setText(mHotOrederDetail.getOrderInfo().getFree_pass() + "串1");
            }
        }
        //倍数
        tv_follow_detail_project_beishu.setText(mHotOrederDetail.getOrderInfo().getMultiple() + "倍");
        if (orderType == 2) {
            list_item_parent_title.setText("投注信息");
            updateFanDanCaseTable(mHotOrederDetail);
        } else {
            try {
                updateCaseTable(mHotOrederDetail);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void updateFanDanCaseTable(FollowOrderDetailBean mHotOrederDetail) {
        int top = 0;
        int height = 0;
        int splitHeight = ScreenUtil.dip2px(this, 1);
        for (int i = 0; i < mHotOrederDetail.getOrderInfo().getBall_info().size(); i++) {
            BallInfoEntity ballInfoEntity = mHotOrederDetail.getOrderInfo().getBall_info().get(i);
            View inflate = null;
            inflate = LayoutInflater.from(this).inflate(R.layout.table_row_layout_2, tb_follow, false);
            tb_follow.addView(inflate);

            TextView one = inflate.findViewById(R.id.tv_one);
            TextView two = inflate.findViewById(R.id.tv_two);
            TextView three = inflate.findViewById(R.id.tv_three);
            TextView four = inflate.findViewById(R.id.tv_four);
            one.setText(ballInfoEntity.getNum());
            //发单详情
            two.setText(Html.fromHtml(ballInfoEntity.getH_cn() + "<br />VS<br />" + ballInfoEntity.getA_cn()));
            one.measure(0,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            top += one.getMeasuredHeight() + splitHeight;
            String plan = mHotOrederDetail.getOrderInfo().getOrigin_num();//投注内容
            String[] touzhuContent = getBetContentAndPrizeContent(plan, ballInfoEntity, mHotOrederDetail.getOrderInfo().getLottery_type());
            three.setText(Html.fromHtml(touzhuContent[0]));
            String bDate = touzhuContent[2];//通过时间+num获取该场比赛的开奖结果
            String key = touzhuContent[3];//投注类型
            String prizeContent = "待开奖";
            if (mHotOrederDetail.getOrderInfo().getLottery_type() == 2) {
//                prizeContent = getPrizeContent(key, ballInfoEntity);
                prizeContent = getLqPrize(ballInfoEntity.getBasketball_info());
            }
            four.setText(Html.fromHtml(prizeContent));
            height += one.getMeasuredHeight() + splitHeight;
            System.out.println("高度" + i + ":" + height);
        }
        rlLock.setVisibility(View.GONE);
    }

    private String getPrizeContent(String key, BallInfoEntity ballInfoEntity) {
        StringBuffer value = new StringBuffer();
        switch (key) {
            case "3010":
                if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                    if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) > Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                        //主胜
                        value.append("主胜,");
                    } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) == Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                        //主胜
                        value.append("主平,");
                    } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) < Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                        //主胜
                        value.append("主负,");
                    }
                }
                break;
            case "3007":
                if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                    value.append(ballInfoEntity.getFinal_score() + ",");
                }
                break;
            case "3006":
                if (!TextUtils.isEmpty(ballInfoEntity.getLet_score())) {
                    value.append(ballInfoEntity.getFinal_score() + ",");
                }
                break;
            case "3008":
                if (!TextUtils.isEmpty(ballInfoEntity.getFinal_score())) {
                    value.append(Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) + Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1]) + "球,");
                }
                break;
            case "3009":
                //总进球
                if (!TextUtils.isEmpty(ballInfoEntity.getHalf_score())) {
                    value.append(ballInfoEntity.getHalf_score().replaceAll("3", "胜").replaceAll("1", "平").replaceAll("0", "负") + ",");
                }
                break;
            case "3011":
                break;
        }
        return value.toString().endsWith(",") ? value.toString().substring(0, value.toString().length() - 1) : value.toString();
    }


    TableLayout mTableLayout;
    TableRow tr_content, tr_title;
    int titleHeight = 0;

    private void updateCaseTable(FollowOrderDetailBean mHotOrederDetail) {
        int top = 0;
        int height = 0;
//        int splitHeight = ScreenUtil.dip2px(this, 1);
        int splitHeight = 1;
        for (int i = 0; i < mHotOrederDetail.getOrderInfo().getBall_info().size(); i++) {
            BallInfoEntity ballInfoEntity = mHotOrederDetail.getOrderInfo().getBall_info().get(i);
            View inflate = null;
            TextView five = null;
            if (orderType != 2) {
                inflate = LayoutInflater.from(this).inflate(R.layout.table_row_layout, mTableLayout, false);
                mTableLayout.addView(inflate);
                five = inflate.findViewById(R.id.tv_five);
            } else {
                inflate = LayoutInflater.from(this).inflate(R.layout.table_row_layout_2, tb_follow, false);
                tb_follow.addView(inflate);
            }

            TextView one = inflate.findViewById(R.id.tv_one);
            TextView two = inflate.findViewById(R.id.tv_two);
            TextView three = inflate.findViewById(R.id.tv_three);
            TextView four = inflate.findViewById(R.id.tv_four);
            one.setText(ballInfoEntity.getNum());
            if (orderType != 2) {
                four.setText(Html.fromHtml(ballInfoEntity.getA_cn()));
                two.setText(Html.fromHtml(ballInfoEntity.getH_cn()));
                three.setText(ballInfoEntity.getFinal_score() != null ? Html.fromHtml(ballInfoEntity.getFinal_score()) : "");
            } else {
                //发单详情
                two.setText(Html.fromHtml(ballInfoEntity.getH_cn() + "<br />VS<br />" + ballInfoEntity.getA_cn()));
//                four.setText(Html.fromHtml(ballInfoEntity.getFinal_score()));
//                four.setText(Html.fromHtml(ballInfoEntity.getA_cn()));
            }
            one.measure(0,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            //根据score有无值来判断是否显示开奖  matchSelect显示开奖数据
            //2 公开   1开奖可见  0永久保密
            if (mHotOrederDetail.getOrderInfo().getPublic_type().equals("2")) {
                if (five != null) {
                    five.setVisibility(View.VISIBLE);
                }
//                five.setText("20:00截止后公开");
                top += one.getMeasuredHeight() + splitHeight;
//                five.setText("投注内容");


                String plan = mHotOrederDetail.getOrderInfo().getOrigin_num();//投注内容

                JSONObject jsonObject = null;
                try {
                    if (!TextUtils.isEmpty(plan)) {
                        jsonObject = new JSONObject(plan);
                        String bDate = ballInfoEntity.getB_date().replaceAll("-", "");
                        String num = ballInfoEntity.getNum_type();
                        JSONObject jsonObject1 = jsonObject.getJSONObject(bDate + num);
                        Iterator<String> iterator = jsonObject1.keys();
                        HashMap<String, String> hashMap = new HashMap<>();
                        while (iterator.hasNext()) {
                            StringBuffer stringBufferValue = new StringBuffer();
                            StringBuffer kaijiang = new StringBuffer();
                            String key = iterator.next();
                            hashMap.put(key, key);
                            JSONObject jsonObjectValue = jsonObject1.getJSONObject(key);
                            Iterator<String> iterator2 = jsonObjectValue.keys();
                            // 3010: 胜平负
                            // 3006:让球胜平负
                            // 3007: 全场比分(90分钟)
                            // 3008: 进球数
                            // 3009: 半全场胜负  这个暂时不支持
                            // 3011: 混合过关 （一注投注中包含2个玩法以上的就是混合过关）
                            switch (key) {
                                case "3010":
                                    while (iterator2.hasNext()) {
                                        String key2 = iterator2.next();//投注种类中所代表的种类
                                        String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                        if (key2.equals("0")) {
                                            stringBufferValue.append("主负,");
                                        }
                                        if (key2.equals("1")) {
                                            stringBufferValue.append("主平,");
                                        }
                                        if (key2.equals("3")) {
                                            stringBufferValue.append("主胜,");
                                        }
                                    }
                                    if (ballInfoEntity.getFinal_score() != null || "null".equals(ballInfoEntity.getFinal_score())) {
                                        if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) > Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                            //主胜
                                            kaijiang.append("主胜,");
                                        } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) == Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                            //主胜
                                            kaijiang.append("主平,");
                                        } else if (Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) < Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1])) {
                                            //主胜
                                            kaijiang.append("主负,");
                                        }
                                    }
                                    break;
                                case "3007"://全场比分
                                    while (iterator2.hasNext()) {
                                        String key2 = iterator2.next();//投注种类中所代表的种类
                                        if (!key2.equals("single")) {
                                            JSONObject value = jsonObjectValue.getJSONObject(key2);//种类所带表的赔率
                                            Iterator<String> iterator3 = value.keys();
                                            while (iterator3.hasNext()) {
                                                String key3 = iterator3.next();
                                                stringBufferValue.append(key3.substring(0, 1) + ":" + key3.substring(1, 2) + ",");
                                            }
                                        }
                                    }
                                    if (ballInfoEntity.getFinal_score() != null || "null".equals(ballInfoEntity.getFinal_score())) {
                                        kaijiang.append(ballInfoEntity.getFinal_score() + ",");
                                    }
                                    break;
                                case "3006": //让球胜平负
                                    while (iterator2.hasNext()) {
                                        String key2 = iterator2.next();//投注种类中所代表的种类
                                        String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                        if (!key2.equals("single")) {
                                            if (key2.equals("0")) {
                                                stringBufferValue.append("让负,");
                                            }
                                            if (key2.equals("1")) {
                                                stringBufferValue.append("让平,");
                                            }
                                            if (key2.equals("3")) {
                                                stringBufferValue.append("让胜,");
                                            }
                                        }
                                    }
                                    if (ballInfoEntity.getLet_score() != null || "null".equals(ballInfoEntity.getLet_score())) {
                                        kaijiang.append(ballInfoEntity.getLet_score() + ",");
                                    }
                                    break;
                                case "3008":
                                    //总进球
                                    while (iterator2.hasNext()) {
                                        String key2 = iterator2.next();//投注种类中所代表的种类
                                        String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                        if (!key2.equals("single")) {
                                            stringBufferValue.append(key2 + "球,");
                                        }
                                    }
                                    if (ballInfoEntity.getFinal_score() != null || "null".equals(ballInfoEntity.getFinal_score())) {
                                        kaijiang.append(Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[0]) + Integer.valueOf(ballInfoEntity.getFinal_score().split(":")[1]) + "球,");
                                    }
                                    break;
                                case "3009":
                                    if (ballInfoEntity.getHalf_score() != null || "null".equals(ballInfoEntity.getHalf_score())) {
                                        kaijiang.append(ballInfoEntity.getHalf_score().replaceAll("3", "胜").replaceAll("1", "平").replaceAll("0", "负"));
                                    }
                                    break;
                            }
                            if (five != null) {
                                if (mHotOrederDetail.getOrderInfo().getLottery_type() == 2) {
                                    five.setText(getLqPrize(ballInfoEntity.getBasketball_info()));
                                } else {
                                    five.setText((kaijiang.toString().endsWith(",") ? (kaijiang.toString().substring(0, kaijiang.length() - 1)) : kaijiang));
                                }
                            } else {
                                three.setText((stringBufferValue.toString().endsWith(",") ? (stringBufferValue.toString().substring(0, stringBufferValue.length() - 1)) : stringBufferValue));
                                four.setText((kaijiang.toString().endsWith(",") ? (kaijiang.toString().substring(0, kaijiang.length() - 1)) : kaijiang));
                            }
//                            break;
                        }
//                                ((TextView) tablerow_content.getChildAt(0)).setText(ballInfoEntity2.getNum());
//                                ((TextView) tablerow_content.getChildAt(1)).setText(ballInfoEntity2.getH_cn());
//                                ((TextView) tablerow_content.getChildAt(2)).setText(ballInfoEntity2.getA_cn());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } else if (mHotOrederDetail.getOrderInfo().getPublic_type().equals("1")) {
                if (five != null) {
                    five.setVisibility(View.INVISIBLE);
                }
                tv_bet_lock_dec.setText(Html.fromHtml(TimeUtils.getFormatTime(1000 * Long.parseLong(mHotOrederDetail.getOrderInfo().getEnd_time()), "MM-dd HH:mm") + "<br />截止后公开"));
                top += one.getMeasuredHeight() + splitHeight;
            } else {
                tv_bet_lock_dec.setText("永久保密");
                if (five != null) {
                    five.setVisibility(View.INVISIBLE);
                }
            }
//            View.inflate(getActivity(),R.layout.table_row_line_layout,mTableLayout);
            height += one.getMeasuredHeight() + splitHeight;
            System.out.println("高度" + i + ":" + height);
        }
        //这里控制覆盖在开奖信息上的锁的位置大小
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlLock.getLayoutParams();
        layoutParams.topMargin = top;
        layoutParams.height = height + height / mHotOrederDetail.getOrderInfo().getBall_info().size() - top;
        System.out.println("高度z总：" + layoutParams.height + "  titleHeight:" + titleHeight + "  line_height" + height);
        rlLock.setLayoutParams(layoutParams);
//        boolean isSecret = false;
//        if (isSecret)
//            tv_bet_lock_dec.setText("保密单");
//        else
        if (top == height)
            rlLock.setVisibility(View.GONE);
    }


    private boolean isCanWatch(FollowOrderDetailBean mHotOrederDetail) {
        //2 公开   1开奖可见  0永久保密
        switch (mHotOrederDetail.getOrderInfo().getPublic_type()) {
            case "0":
                return false;
            case "1":
                return true;
            case "2":
                return true;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_follow_order_detail_go_on:
                if (type==1) {
                    startActivity(new Intent(this, FootballMainActivity.class));
                }else{
                    startActivity(new Intent(this, BascketballMainActivity.class));
                }
                break;
            case R.id.tv_bet://获取倍数
                int orderId = 0;
                if (followOrderDetailBean != null) {
                    orderId = Integer.parseInt(followOrderDetailBean.getOrderInfo().getId());
                } else if (myFollowDetailBean != null) {
                    orderId = Integer.parseInt(myFollowDetailBean.getPlan_info().getId());
                }
                if (orderId == 0) {
                    ByAlert.alert("当前方案有误，请刷新后重试");
                    return;
                }
                userPresenter.followCase(orderId, multiple);
                break;
        }
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.getDaShenFollowDetail_SUCCESS || requestSuccessType == userPresenter.getFaDanDetail_SUCCESS) {
//            String json="{\"follow_user\":{\"follow_user\":[],\"follow_num\":0,\"amount\":\"0\"},\"orderInfo\":{\"id\":\"18\",\"user_id\":\"42\",\"type\":\"0\",\"lottery_type\":\"1\",\"lottery_id\":\"247\",\"category_id\":\"0\",\"end_time\":\"1538283600\",\"create_time\":\"1538274377\",\"public_type\":\"2\",\"reason\":\"必需\",\"total_amount\":\"400\",\"low_amount\":\"100\",\"is_result\":1,\"commission\":\"0\",\"lottery_result\":\"0\",\"single_amount\":\"8\",\"ensure_odds\":\"100\",\"earn_rate\":\"0\",\"win_amount\":\"0\",\"plays\":\"20180930002,20180930001\",\"plan\":\"[{\\\"bonus\\\":11.9,\\\"content\\\":\\\"20180930001-3006-0-183|20180930002-3006-1-325\\\",\\\"multiple\\\":1,\\\"pass_type\\\":3006,\\\"strung\\\":2},{\\\"bonus\\\":13.0,\\\"content\\\":\\\"20180930001-3006-0-183|20180930002-3010-1-355\\\",\\\"multiple\\\":1,\\\"pass_type\\\":3011,\\\"strung\\\":2},{\\\"bonus\\\":23.72,\\\"content\\\":\\\"20180930001-3010-1-365|20180930002-3006-1-325\\\",\\\"multiple\\\":1,\\\"pass_type\\\":3011,\\\"strung\\\":2},{\\\"bonus\\\":25.92,\\\"content\\\":\\\"20180930001-3010-1-365|20180930002-3010-1-355\\\",\\\"multiple\\\":1,\\\"pass_type\\\":3010,\\\"strung\\\":2}]\",\"is_display\":\"0\",\"is_recommend\":\"0\",\"nickname\":\"\uE04B\",\"head_pic\":\"http://rrgc.8raw.com/Uploads/avatar/2018-09-29/5baf52a7da784.jpg\",\"ball_info\":[{\"num\":\"周日002\",\"num_type\":\"002\",\"b_date\":\"2018-09-30\",\"l_id\":\"43\",\"l_cn\":\"日本乙级联赛\",\"h_id\":\"1746\",\"h_cn\":\"赞岐釜玉海\",\"a_id\":\"1489\",\"a_cn\":\"町田泽维亚\",\"half_score\":33,\"final_score\":\"3:2\",\"let_score\":\"3\",\"h_cn_score\":\"3\",\"a_cn_score\":\"2\",\"p_time\":\"20180930002\"},{\"num\":\"周日001\",\"num_type\":\"001\",\"b_date\":\"2018-09-30\",\"l_id\":\"42\",\"l_cn\":\"日本职业联赛\",\"h_id\":\"560\",\"h_cn\":\"浦和红钻\",\"a_id\":\"552\",\"a_cn\":\"柏太阳神\",\"half_score\":31,\"final_score\":\"5:4\",\"let_score\":\"1\",\"h_cn_score\":\"5\",\"a_cn_score\":\"4\",\"p_time\":\"20180930001\"}],\"free_pass\":\"2*1\",\"multiple\":\"50\",\"red_num\":0,\"origin_num\":\"{\\\"20180930002\\\":{\\\"3010\\\":{\\\"1\\\":\\\"3.55\\\",\\\"single\\\":0},\\\"3006\\\":{\\\"1\\\":\\\"3.25\\\",\\\"single\\\":0}},\\\"20180930001\\\":{\\\"3010\\\":{\\\"1\\\":\\\"3.65\\\",\\\"single\\\":1},\\\"3006\\\":{\\\"0\\\":\\\"1.83\\\",\\\"single\\\":0}}}\",\"is_my_plan\":1,\"earn_money\":\"-\",\"ticket_status\":\"2\",\"pay_status\":\"1\",\"sn\":\"FBS42201809301026175312\"}}";
//            followOrderDetailBean=new Gson().fromJson(json,FollowOrderDetailBean.class);
            followOrderDetailBean = (FollowOrderDetailBean) o;
            updateDaShenUI(followOrderDetailBean);
            if (orderType == 2) {
                setOrderMessage(followOrderDetailBean);
            } else if (orderType == 0) {//大神跟单详情

            }
        }
        if (requestSuccessType == userPresenter.getFollowDetail_SUCCESS) {
            myFollowDetailBean = (MyFollowDetailBean) o;
            updateMyGenDanUI(myFollowDetailBean);
            setOrderMessage(myFollowDetailBean);
        }
        if (requestSuccessType == userPresenter.followCase_SUCCESS) {
            OrderBean orderBean = (OrderBean) o;
            PayDialog payDialog = new PayDialog(this, true);
            payDialog.setOrderBean(orderBean);
            payDialog.show();
        }
    }

    private void setOrderMessage(Object object) {
        ll_order_message.setVisibility(View.VISIBLE);
        if (object instanceof FollowOrderDetailBean) {
            ((TextView) ll_order_message.getChildAt(0)).setText("订单编号:" + followOrderDetailBean.getOrderInfo().getSn());
            ((TextView) ll_order_message.getChildAt(1)).setText("下单时间:" + TimeUtils.getFormatTime(Long.parseLong(followOrderDetailBean.getOrderInfo().getCreate_time()) * 1000, "yyyy-MM-dd HH:mm:ss"));
        } else if (object instanceof MyFollowDetailBean) {
            ((TextView) ll_order_message.getChildAt(0)).setText("订单编号:" + myFollowDetailBean.getPlan_info().getSn());
            ((TextView) ll_order_message.getChildAt(1)).setText("下单时间:" + TimeUtils.getFormatTime(Long.parseLong(myFollowDetailBean.getPlan_info().getCreate_time()) * 1000, "yyyy-MM-dd HH:mm:ss"));
        }
        ll_order_message.getChildAt(3).setOnClickListener(view -> {
            //出票明细
        });
    }

    private void updateMyGenDanUI(MyFollowDetailBean myFollowDetailBean) {
        tv_end_time.setText("");
        tv_end_time.setBackgroundResource(0);
        //中奖状态显示
        switch (myFollowDetailBean.getPlan_info().getIs_result()) {
            case "1":
                switch (myFollowDetailBean.getPlan_info().getLottery_result()) {
                    case "0":
                        tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.zaijiezaili_follow_detail, 0, 0, 0);
                        break;
                    case "1":
                        tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.gongxizhongjiang, 0, 0, 0);
                        break;
                }
                btn_follow_order_detail_go_on.setVisibility(View.VISIBLE);
                ll_follow_container.setVisibility(View.GONE);
                break;
            case "0":
                ll_follow_container.setVisibility(View.VISIBLE);
                btn_follow_order_detail_go_on.setVisibility(View.GONE);
                tv_end_time.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.dengdaikaijian, 0, 0, 0);
                break;
        }
        tv_username.setText(myFollowDetailBean.getPlan_info().getNickname());
        GlideUtil.displayForCircle(this, myFollowDetailBean.getPlan_info().getHead_pic(), R.mipmap.head_default, iv_user_icon);
        tv_follow_beizhu.setText(Html.fromHtml("跟单于:<font color='red'>" + myFollowDetailBean.getPlan_info().getNickname() + "的方案【" + myFollowDetailBean.getPlan_info().getSn() + "</font>"));
        if (myFollowDetailBean.getPlan_info().getIs_result().equals("0")) {
            tv_group_buy_money.setText("待开奖");//奖金收入
            tv_status.setText("待开奖");//佣金扣点
            tv_jiangjin.setText("待开奖");//佣金金额
            tv_shijishouru.setText("待开奖");//实际收入
        } else {
            tv_group_buy_money.setText(myFollowDetailBean.getUser_info().getAmount() + "元");//奖金收入
            tv_status.setText(myFollowDetailBean.getUser_info().getRate() + "%");//佣金扣点
            tv_jiangjin.setText((myFollowDetailBean.getUser_info().getCommission() == null ? "0" : myFollowDetailBean.getUser_info().getCommission()) + "元");//佣金金额
            tv_shijishouru.setText(myFollowDetailBean.getUser_info().getIncome() + "元");//实际收入
        }
        btn_follow_order_detail_go_on.setText("继续购买" + (type == 1 ? "竞彩足球" : "竞彩篮球"));
        iv_status.setBackgroundResource(Integer.valueOf(getStatus(myFollowDetailBean.getPlan_info().getTicket_status() + "")[1]));
        iv_status.setVisibility(View.VISIBLE);
        updateMyFollowCaseDetail();
    }

    private void updateMyFollowCaseDetail() {
        list_item_parent_title.setText("投注详情");
        //场数
        tv_follow_detail_project_times.setText((myFollowDetailBean.getPlan_info().getBall_info() == null ? 0 : myFollowDetailBean.getPlan_info().getBall_info().size()) + "场");
        //投注方式
        if (myFollowDetailBean.getPlan_info().getFree_pass().contains("*")) {
            tv_follow_detail_project_mode.setText(myFollowDetailBean.getPlan_info().getFree_pass().replaceAll("\\*", "串"));
        } else {
            if (myFollowDetailBean.getPlan_info().getFree_pass().equals("1")) {
                tv_follow_detail_project_mode.setText("单关");
            } else if (myFollowDetailBean.getPlan_info().getFree_pass().contains(",")) {
                tv_follow_detail_project_mode.setText(myFollowDetailBean.getPlan_info().getFree_pass().replaceAll(",", "串1,"));
            } else {
                tv_follow_detail_project_mode.setText(myFollowDetailBean.getPlan_info().getFree_pass() + "串1");
            }
        }
        //倍数
        tv_follow_detail_project_beishu.setText(myFollowDetailBean.getPlan_info().getMultiple() + "倍");
        updateMyFollowCaseTable();
    }

    TableLayout tb_shendan, tb_follow;

    private void updateMyFollowCaseTable() {
        tb_shendan.setVisibility(View.GONE);
        tb_follow.setVisibility(View.VISIBLE);
        int top = 0;
        int height = 0;
        int splitHeight = ScreenUtil.dip2px(this, 1);
        for (int i = 0; i < myFollowDetailBean.getPlan_info().getBall_info().size(); i++) {
            BallInfoEntity ballInfoEntity = myFollowDetailBean.getPlan_info().getBall_info().get(i);
            View inflate = LayoutInflater.from(this).inflate(R.layout.table_row_layout_2, tb_follow, false);
            tb_follow.addView(inflate);

            TextView one = inflate.findViewById(R.id.tv_one);
            TextView two = inflate.findViewById(R.id.tv_two);
            TextView three = inflate.findViewById(R.id.tv_three);
            TextView four = inflate.findViewById(R.id.tv_four);
//            TextView five = inflate.findViewById(R.id.tv_five);
            one.setText(ballInfoEntity.getNum());
            two.setText(Html.fromHtml(ballInfoEntity.getH_cn() + "<br />vs<br />" + ballInfoEntity.getA_cn()));
//            three.setText("打分"+ballInfoEntity.getFinal_score() != null ? Html.fromHtml(ballInfoEntity.getFinal_score()) : "");
//            four.setText(Html.fromHtml(ballInfoEntity.getA_cn()));
            one.measure(0,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            //根据score有无值来判断是否显示开奖  matchSelect显示开奖数据
            //2 公开   1开奖可见  0永久保密
//            if (mHotOrederDetail.getOrderInfo().getPublic_type().equals("2")) {
//                five.setVisibility(View.GONE);
//                five.setText("20:00截止后公开");
            top += one.getMeasuredHeight() + splitHeight;
//                five.setText("投注内容");
            String plan = myFollowDetailBean.getPlan_info().getOrigin_num();//投注内容
            if (TextUtils.isEmpty(plan)) {
                three.setText("保密单");
                four.setText("保密单");
            } else {
                String[] touzhuContent = getBetContentAndPrizeContent(plan, ballInfoEntity, myFollowDetailBean.getPlan_info().getLottery_type());
                three.setText(Html.fromHtml(touzhuContent[0]));
                four.setText(Html.fromHtml(touzhuContent[1]));
            }
//            JSONObject jsonObject = null;
//            try {
//                if (!TextUtils.isEmpty(plan)) {
//                    jsonObject = new JSONObject(plan);
//                    String bDate = ballInfoEntity.getB_date().replaceAll("-", "");
//                    String num = ballInfoEntity.getNum_type();
//                    JSONObject jsonObject1 = jsonObject.getJSONObject(bDate + num);
//                    Iterator<String> iterator = jsonObject1.keys();
//                    HashMap<String, String> hashMap = new HashMap<>();
//                    while (iterator.hasNext()) {
//                        StringBuffer stringBufferValue = new StringBuffer();
//                        StringBuffer kaijiang = new StringBuffer();
//                        String key = iterator.next();
//                        hashMap.put(key, key);
//                        JSONObject jsonObjectValue = jsonObject1.getJSONObject(key);
//                        Iterator<String> iterator2 = jsonObjectValue.keys();
//                        // 3010: 胜平负
//                        // 3006:让球胜平负
//                        // 3007: 全场比分(90分钟)
//                        // 3008: 进球数
//                        // 3009: 半全场胜负  这个暂时不支持
//                        // 3011: 混合过关 （一注投注中包含2个玩法以上的就是混合过关）
//                        switch (key) {
//                            case "3010":
//                                while (iterator2.hasNext()) {
//                                    String key2 = iterator2.next();//投注种类中所代表的种类
//                                    String value = jsonObjectValue.getString(key2);//种类所带表的赔率
//                                    if (key2.equals("0")) {
//                                        stringBufferValue.append("负,");
//                                    }
//                                    if (key2.equals("1")) {
//                                        stringBufferValue.append("平,");
//                                    }
//                                    if (key2.equals("3")) {
//                                        stringBufferValue.append("胜,");
//                                    }
//                                }
//                                break;
//                            case "3007": //让球胜平负
//                                while (iterator2.hasNext()) {
//                                    String key2 = iterator2.next();//投注种类中所代表的种类
//                                    if (!key2.equals("single")) {
//                                        JSONObject value = jsonObjectValue.getJSONObject(key2);//种类所带表的赔率
//                                        Iterator<String> iterator3 = value.keys();
//                                        while (iterator3.hasNext()) {
//                                            String key3 = iterator3.next();
//                                            stringBufferValue.append(key3.substring(0, 1) + ":" + key3.substring(1, 2) + ",");
//                                        }
//                                    }
//                                }
//                                break;
//                            case "3006":
//                                while (iterator2.hasNext()) {
//                                    String key2 = iterator2.next();//投注种类中所代表的种类
//                                    String value = jsonObjectValue.getString(key2);//种类所带表的赔率
//                                    if (!key2.equals("single")) {
//                                        stringBufferValue.append(key2 + "球,");
//                                    }
//                                }
//                                break;
//                            case "3008":
//                                //总进球
//                                while (iterator2.hasNext()) {
//                                    String key2 = iterator2.next();//投注种类中所代表的种类
//                                    String value = jsonObjectValue.getString(key2);//种类所带表的赔率
//                                    if (!key2.equals("single")) {
//                                        stringBufferValue.append(key2 + "球,");
//                                    }
//                                }
//                                break;
//                            case "3009":
//                                break;
//                            case "3011":
//                                break;
//                        }
//                        three.setText((stringBufferValue.toString().endsWith(",") ? (stringBufferValue.toString().substring(0, stringBufferValue.length() - 1)) : stringBufferValue));
//                        four.setText((kaijiang.toString().endsWith(",") ? (kaijiang.toString().substring(0, kaijiang.length() - 1)) : kaijiang));
//                        four.setText((stringBufferValue.toString().endsWith(",") ? (stringBufferValue.toString().substring(0, stringBufferValue.length() - 1)) : stringBufferValue));
//                        break;
//                    }
////                                if (hashMap.size() > 1) {//混合
////                                    viewHolderCaseJinE.tv_lottery_type.setText("足球混合投注");
////                                } else if (hashMap.size() == 1) {//单种投注
////                                    if (hashMap.containsKey("3010")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("胜平负");
////                                    } else if (hashMap.containsKey("3006")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("让球胜平负");
////
////                                    } else if (hashMap.containsKey("3007")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("全场比分");
////
////                                    } else if (hashMap.containsKey("3008")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("进球数");
////
////                                    } else if (hashMap.containsKey("3009")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("半场胜平负");
////
////                                    } else if (hashMap.containsKey("3011")) {
////                                        viewHolderCaseJinE.tv_lottery_type.setText("足球混合投注");
////
////                                    }
//////                                viewHolderCaseJinE.tv_lottery_type.setText("足球混合投注");
////                                }
////                                ((TextView) tablerow_content.getChildAt(0)).setText(ballInfoEntity2.getNum());
////                                ((TextView) tablerow_content.getChildAt(1)).setText(ballInfoEntity2.getH_cn());
////                                ((TextView) tablerow_content.getChildAt(2)).setText(ballInfoEntity2.getA_cn());
//
////                    }
////                    break;
//                } else {
//
//                    three.setText("空的1");
//                    three.setText("空的2");
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }


//            } else if (mHotOrederDetail.getOrderInfo().getPublic_type().equals("1")) {
//                five.setVisibility(View.INVISIBLE);
//                tv_bet_lock_dec.setText(Html.fromHtml(TimeUtils.getFormatTime(1000 * Long.parseLong(mHotOrederDetail.getOrderInfo().getEnd_time()), "MM-dd HH:mm") + "<br />截止后公开"));
//                top += one.getMeasuredHeight() + splitHeight;
//            } else {
//                tv_bet_lock_dec.setText("永久保密");
//                five.setVisibility(View.INVISIBLE);
//            }
//            View.inflate(getActivity(),R.layout.table_row_line_layout,mTableLayout);
//            height += one.getMeasuredHeight() + splitHeight;
//            System.out.println("高度" + i + ":" + height);
        }
        //这里控制覆盖在开奖信息上的锁的位置大小
//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlLock.getLayoutParams();
//        layoutParams.topMargin = top;
//        layoutParams.height = height + height / mHotOrederDetail.getOrderInfo().getBall_info().size() - top;
//        System.out.println("高度z总：" + layoutParams.height + "  titleHeight:" + titleHeight + "  line_height" + height);
//        rlLock.setLayoutParams(layoutParams);
////        boolean isSecret = false;
////        if (isSecret)
////            tv_bet_lock_dec.setText("保密单");
////        else
//        if (top == height)
//            rlLock.setVisibility(View.GONE);
    }


    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        if (requestErrorType == userPresenter.getDaShenFollowDetail_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.followCase_FAIL) {
            ByAlert.alert(msg);
        }
    }
}
